package bootstrap

import (
	"fmt"
	"go.getaway.adminspro.padmins.com/handler/admin/admin"
	"go.getaway.adminspro.padmins.com/handler/admin/log"
	"go.getaway.adminspro.padmins.com/handler/admin/resource"
	"go.getaway.adminspro.padmins.com/handler/admin/role"
	"go.getaway.adminspro.padmins.com/handler/auth"
	"go.getaway.adminspro.padmins.com/untils"
	"go.getaway.adminspro.padmins.com/untils/config"
	"go.getaway.adminspro.padmins.com/untils/db"
	"go.getaway.adminspro.padmins.com/untils/di"
	"go.getaway.adminspro.padmins.com/untils/env"
	"go.getaway.adminspro.padmins.com/untils/handler"
	"go.getaway.adminspro.padmins.com/untils/response"
	"strconv"
)

func regConfig(Di *di.Di) {
	Evn := &env.Env{}
	Evn.FromFile(".env")
	Config := &config.Config{}
	Config.Set("app.debug", Evn.Get("APP_DEBUG", "1"))
	Config.Set("http.addr", Evn.Get("HTTP_ADDR", ":4000"))
	Config.Set("db.driver", Evn.Get("DB_DRIVER", "mysql"))
	Config.Set("db.host", Evn.Get("DB_HOST", "127.0.0.1"))
	Config.Set("db.port", Evn.Get("DB_PORT", "3306"))
	Config.Set("db.dbname", Evn.Get("DB_DBNAME", ""))
	Config.Set("db.charset", Evn.Get("DB_CHARSET", "utf8"))
	Config.Set("db.username", Evn.Get("DB_USERNAME", ""))
	Config.Set("db.password", Evn.Get("DB_PASSWORD", ""))
	Config.Set("db.maxopenconns", Evn.Get("DB_MAXOPENCONNS", "1"))
	Di.Add("config", Config)
}

func regDb(Di *di.Di) {
	Config := Di.Get("config").(*config.Config)
	maxopenconns, e := strconv.Atoi(Config.Get("db.maxopenconns"))

	if e != nil {
		panic(fmt.Sprintf("db.maxopenconns参数类型错误%v", e))
	}

	Di.Add("dbConnect", db.Open(Config.Get("db.driver"), Config.Get("db.username"), Config.Get("db.password"), Config.Get("db.host"), Config.Get("db.port"), Config.Get("db.dbname"), Config.Get("db.charset"), maxopenconns))
}

func Run() *di.Di {
	Di := di.Instance()
	regConfig(Di)
	regDb(Di)
	Di.Add("response", &response.Response{Di: Di})

	Di.Add("routersPermission", map[string]string{
		"auth.login":               "public",
		"auth.getPicCaptchaConfig": "public",
		"auth.logout":              "public",
		"auth.getInfo":             "allow",
	})

	Di.Add("routers", map[string]func(handler *handler.Handler) (map[string]interface{}, untils.Error){
		// 账号登录
		"auth.login":               auth.Login,
		"auth.logout":              auth.Logout,
		"auth.getPicCaptchaConfig": auth.GetPicCaptchaConfig,
		"auth.getInfo":             auth.GetInfo,
		// 管理员管理
		"admin.admin":        admin.Index,
		"admin.admin.index":  admin.Index,
		"admin.admin.list":   admin.List,
		"admin.admin.detail": admin.Detail,
		"admin.admin.add":    admin.Add,
		"admin.admin.edit":   admin.Edit,
		"admin.admin.del":    admin.Del,
		"admin.admin.multi":  admin.Multi,
		// 角色组
		"admin.role":          role.Index,
		"admin.role.index":    role.Index,
		"admin.role.list":     role.List,
		"admin.role.detail":   role.Detail,
		"admin.role.add":      role.Add,
		"admin.role.edit":     role.Edit,
		"admin.role.del":      role.Del,
		"admin.role.multi":    role.Multi,
		"admin.role.resource": role.Resource,
		// 菜单资源管理
		"admin.resource":        resource.Index,
		"admin.resource.index":  resource.Index,
		"admin.resource.list":   resource.List,
		"admin.resource.detail": resource.Detail,
		"admin.resource.add":    resource.Add,
		"admin.resource.edit":   resource.Edit,
		"admin.resource.del":    resource.Del,
		"admin.resource.multi":  resource.Multi,
		// 操作日志
		"admin.log":        log.Index,
		"admin.log.index":  log.Index,
		"admin.log.detail": log.Detail,
	})

	return Di
}
